package com.sqf.soft.service.impl;

import com.sqf.soft.dao.IClassifyDao;
import com.sqf.soft.entity.Classify;
import com.sqf.soft.service.IClassifyService;
import com.sqf.soft.utils.CommonUtils;
import com.sqf.soft.utils.PageBean;
import com.sqf.soft.utils.ResultMap;
import com.sqf.soft.utils.SystemConstant;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;

@Service
public class ClassifyService implements IClassifyService {

    private final IClassifyDao classifyDao;

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    public ClassifyService(IClassifyDao classifyDao) {
        this.classifyDao = classifyDao;
    }

    @Override
    public Map<String, Object> findClassifyAll() {
        try {
            List<Classify> classifyList = classifyDao.findClassifyAll();
            return ResultMap.getList(classifyList);
        } catch (Exception e) {
            log.error("ClassifyService/findClassifyAll", e);
            return ResultMap.getList(SystemConstant.SELECT_EXCEPTION);
        }
    }

    @Override
    public Map<String, Object> findClassifyTree() {
        try {
            List<Classify> classifyList = classifyDao.findClassifyTree();
            JSONArray jsonArray = formatterClassifyTree(classifyList);
            return ResultMap.getTree(jsonArray);
        } catch (Exception e) {
            log.error("ClassifyService/findClassifyTree", e);
            return ResultMap.getList(SystemConstant.SELECT_EXCEPTION);
        }
    }

    private JSONArray formatterClassifyTree(List<Classify> classifyList) {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject;
        for (Classify classify : classifyList) {
            jsonObject = JSONObject.fromObject(classify);
            if (classify.getClassifyList().size() > SystemConstant.NO) {
                JSONArray array = formatterClassifyTree(classify.getClassifyList());
                jsonObject.put("classifyList", array);
            } else {
                jsonObject.remove("classifyList");
            }
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @Override
    public Map<String, Object> findClassifyByPage(PageBean pageBean) {
        try {
            List<Classify> classifyList = classifyDao.findClassifyByPage(pageBean);
            int amount = classifyDao.findAmountByPage(pageBean);
            return ResultMap.getPage(amount, classifyList);
        } catch (Exception e) {
            log.error("ClassifyService/findClassifyByPage", e);
            return ResultMap.getPage(SystemConstant.SELECT_EXCEPTION);
        }
    }

    @Override
    public Map<String, Object> createClassify(Classify classify) {
        if (CommonUtils.isBlank(classify.getName())) {
            return ResultMap.getObject(SystemConstant.CLASSIFY_NAME_IS_BLANK);
        }
        classify.setId(CommonUtils.createKeyId());
        try {
            if (CommonUtils.isBlank(classify.getParentId())) {
                classify.setLevel(SystemConstant.CLASSIFY_INIT_LEVEL);
                classify.setPath(classify.getId());
            } else {
                Classify parentClassify = classifyDao.findClassifyById(classify.getParentId());
                if (parentClassify == null) {
                    return ResultMap.operate(SystemConstant.CLASSIFY_PARENT_IS_NULL);
                }
                classify.setLevel(parentClassify.getLevel() + 1);
                classify.setPath(parentClassify.getPath() + "-" + classify.getId());
            }
            int code = classifyDao.createClassify(classify);
            if (code == SystemConstant.NO) {
                return ResultMap.operate(SystemConstant.INSERT_EXCEPTION);
            }
            return ResultMap.operate(SystemConstant.SUCCESS);
        } catch (Exception e) {
            log.error("ClassifyService/createClassify", e);
            return ResultMap.operate(SystemConstant.INSERT_EXCEPTION);
        }
    }

    @Override
    public Map<String, Object> updateClassify(Classify classify) {
        if (CommonUtils.isBlank(classify.getName())) {
            return ResultMap.getObject(SystemConstant.CLASSIFY_NAME_IS_BLANK);
        }
        try {
            if (CommonUtils.isBlank(classify.getParentId())) {
                classify.setLevel(SystemConstant.CLASSIFY_INIT_LEVEL);
                classify.setPath(classify.getId());
            } else {
                Classify parentClassify = classifyDao.findClassifyById(classify.getParentId());
                if (parentClassify == null) {
                    return ResultMap.operate(SystemConstant.CLASSIFY_PARENT_IS_NULL);
                }
                classify.setLevel(parentClassify.getLevel() + 1);
                classify.setPath(parentClassify.getPath() + "-" + classify.getId());
            }
            int code = classifyDao.updateClassify(classify);
            if (code == SystemConstant.NO) {
                return ResultMap.operate(SystemConstant.UPDATE_EXCEPTION);
            }
            return ResultMap.operate(SystemConstant.SUCCESS);
        } catch (Exception e) {
            log.error("ClassifyService/updateClassify", e);
            return ResultMap.operate(SystemConstant.UPDATE_EXCEPTION);
        }
    }

    @Transactional
    @Override
    public Map<String, Object> deleteClassify(Classify classify) {
        try {
            classifyDao.deleteStatementByClassifyId(classify.getId());
            classifyDao.deleteClassifyByParentId(classify.getId());
            int code = classifyDao.deleteClassify(classify);
            if (code == SystemConstant.NO) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResultMap.operate(SystemConstant.DELETE_EXCEPTION);
            }
            return ResultMap.operate(SystemConstant.SUCCESS);
        } catch (Exception e) {
            log.error("ClassifyService/deleteClassify", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultMap.operate(SystemConstant.DELETE_EXCEPTION);
        }
    }
}
